package admin.servlet;

import admin.dao.Addao;
import admin.entity.Adenty;
import admin.utils.JBDCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

@WebServlet("/delservlet")
public class delservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String incid = req.getParameter("incid");
        String incpwd = req.getParameter("incpwd");
        System.out.println("删除的账号:" + incid);
        System.out.println("删除的密码" + incpwd);

        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();

        JBDCUtil db = new JBDCUtil();
        // 创建一个用户保存下将密码和用户名保存
        Adenty adenty = new Adenty(incid,incpwd);

        Addao dao = new Addao();
        Connection conn = db.getConn();
        try {
            if (dao.deletedemo(conn, adenty)) {
                out.println(dao.deletedemo(conn,adenty));
                out.println("<h1>账号或者密码错误，验证失败</h1>");
                resp.setHeader("Refresh", "1;url=deladinfo.jsp");
            } else {
                out.println(dao.deletedemo(conn,adenty));
                out.println("<h1>删除成功</h1>");
                resp.setHeader("Refresh", "1;url=Adlist.jsp");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            out.close();
        }
    }
}
